// Decompiled by GOOSIN CO,LTD Copyright 2010 Pavel Kouznetsov.
// home page: http://www.Goosin.net
// Decompiler options: packimports(3) fieldsfirst ansi space 
// Source File Name:   invoice_check_ok.java

package draft.purchase;

import include.nseer_cookie.GetWorkflow;
import include.nseer_cookie.counter;
import include.nseer_db.nseer_db_backup1;
import java.io.IOException;
import java.sql.ResultSet;
import java.util.*;
import javax.servlet.ServletException;
import javax.servlet.http.*;
import javax.servlet.jsp.JspFactory;
import validata.ValidataNumber;

public class invoice_check_ok extends HttpServlet
{

	public invoice_check_ok()
	{
	}

	public synchronized void service(HttpServletRequest request, HttpServletResponse response)
		throws IOException, ServletException
	{
		HttpSession dbSession = request.getSession();
		JspFactory _jspxFactory = JspFactory.getDefaultFactory();
		javax.servlet.jsp.PageContext pageContext = _jspxFactory.getPageContext(this, request, response, "", true, 8192, true);
		javax.servlet.ServletContext dbApplication = dbSession.getServletContext();
		try
		{
			HttpSession session = request.getSession();
			nseer_db_backup1 purchase_db = new nseer_db_backup1(dbApplication);
			if (purchase_db.conn((String)dbSession.getAttribute("unit_db_name")))
			{
				ValidataNumber validata = new ValidataNumber();
				counter count = new counter(dbApplication);
				String purchase_ID = request.getParameter("purchase_ID");
				String register = request.getParameter("register");
				String register_time = request.getParameter("register_time");
				String register_id = request.getParameter("register_id");
				String config_ID = request.getParameter("config_ID");
				String invoice_time = request.getParameter("invoice_time");
				String product_ID = request.getParameter("product_ID");
				String product_name = request.getParameter("product_name");
				String demand_cost_price_sum_all = request.getParameter("demand_cost_price_sum_all");
				String real_invoiced_sum1 = request.getParameter("real_invoiced_sum");
				String provider_name[] = request.getParameterValues("provider_name");
				String id[] = request.getParameterValues("id");
				String provider_ID[] = request.getParameterValues("provider_ID");
				String real_contact_person[] = request.getParameterValues("real_contact_person");
				String real_contact_person_tel[] = request.getParameterValues("real_contact_person_tel");
				String demand_cost_price_sum[] = request.getParameterValues("demand_cost_price_sum");
				String invoiced_sum[] = request.getParameterValues("invoiced_sum");
				String invoicing_sum[] = request.getParameterValues("invoicing_sum");
				String remark[] = request.getParameterValues("remark");
				double real_invoiced_sum = Double.parseDouble(real_invoiced_sum1);
				int p = 0;
				String provider_ID_control = provider_ID[0];
				double invoice_control = 0.0D;
				for (int i = 0; i < provider_ID.length; i++)
					if (!invoicing_sum[i].equals(""))
					{
						StringTokenizer tokenTO1 = new StringTokenizer(invoicing_sum[i], ",");
						String invoicing_sum2;
						String invoicing_sum1;
						for (invoicing_sum2 = ""; tokenTO1.hasMoreTokens(); invoicing_sum2 = (new StringBuilder(String.valueOf(invoicing_sum2))).append(invoicing_sum1).toString())
							invoicing_sum1 = tokenTO1.nextToken();

						if (!validata.validata(invoicing_sum2))
							p++;
						if (real_contact_person[i].indexOf("'") != -1 || real_contact_person[i].indexOf("\"") != -1 || real_contact_person[i].indexOf(",") != -1 || real_contact_person_tel[i].indexOf("'") != -1 || real_contact_person_tel[i].indexOf("\"") != -1 || real_contact_person_tel[i].indexOf(",") != -1 || remark[i].indexOf("'") != -1 || remark[i].indexOf("\"") != -1 || remark[i].indexOf(",") != -1 || real_contact_person[i].length() > 30 || real_contact_person_tel[i].length() > 30)
							p++;
					}

				if (p == 0)
				{
					int n = 0;
					for (int i = 0; i < provider_ID.length; i++)
						if (!invoicing_sum[i].equals(""))
							n++;

					if (n != 0)
					{
						String sql8 = (new StringBuilder("select * from purchase_purchasing where purchase_ID='")).append(purchase_ID).append("' and check_tag='0' and invoice_time='").append(invoice_time).append("'").toString();
						ResultSet rs8 = purchase_db.executeQuery(sql8);
						if (rs8.next())
						{
							boolean flag = false;
							List rsList = GetWorkflow.getList(purchase_db, "purchase_config_workflow", "08");
							if (rsList.size() == 0)
							{
								flag = true;
								for (int i = 0; i < provider_ID.length; i++)
								{
									String sql = (new StringBuilder("update purchase_purchasing set check_tag='1' where id='")).append(id[i]).append("' and check_tag='0'").toString();
									purchase_db.executeUpdate(sql);
								}

							}
							provider_ID_control = provider_ID[0];
							double invoiced_sum_control = Double.parseDouble(invoiced_sum[0]);
							for (int i = 0; i < provider_ID.length; i++)
							{
								if (invoicing_sum[i].equals(""))
									invoicing_sum[i] = "0";
								StringTokenizer tokenTO = new StringTokenizer(invoicing_sum[i], ",");
								String invoicing_sum2;
								String invoicing_sum1;
								for (invoicing_sum2 = ""; tokenTO.hasMoreTokens(); invoicing_sum2 = (new StringBuilder(String.valueOf(invoicing_sum2))).append(invoicing_sum1).toString())
									invoicing_sum1 = tokenTO.nextToken();

								if (provider_ID[i].equals(provider_ID_control))
								{
									if (Double.parseDouble(invoicing_sum2) != 0.0D)
									{
										invoiced_sum_control += Double.parseDouble(invoicing_sum2);
										real_invoiced_sum += Double.parseDouble(invoicing_sum2);
										String sql = (new StringBuilder("update purchase_purchasing set real_contact_person='")).append(real_contact_person[i]).append("',real_contact_person_tel='").append(real_contact_person_tel[i]).append("',invoicing_sum='").append(invoicing_sum2).append("',remark='").append(remark[i]).append("',register='").append(register).append("',register_time='").append(register_time).append("' where id='").append(id[i]).append("'").toString();
										purchase_db.executeUpdate(sql);
										if (flag)
											if (invoiced_sum_control == Double.parseDouble(demand_cost_price_sum[i]))
											{
												String sql1 = (new StringBuilder("update purchase_details set invoiced_sum='")).append(invoiced_sum_control).append("',uninvoice_sum='0',invoice_tag='1' where purchase_ID='").append(purchase_ID).append("' and provider_ID='").append(provider_ID[i]).append("'").toString();
												purchase_db.executeUpdate(sql1);
											} else
											{
												double uninvoice_sum = Double.parseDouble(demand_cost_price_sum[i]) - invoiced_sum_control;
												String sql2 = (new StringBuilder("update purchase_details set invoiced_sum='")).append(invoiced_sum_control).append("',uninvoice_sum='").append(uninvoice_sum).append("',invoice_check_tag='0' where purchase_ID='").append(purchase_ID).append("' and provider_ID='").append(provider_ID[i]).append("'").toString();
												purchase_db.executeUpdate(sql2);
											}
									} else
									if (Double.parseDouble(invoicing_sum2) == 0.0D)
									{
										String sql7 = (new StringBuilder("delete from purchase_purchasing where id='")).append(id[i]).append("'").toString();
										purchase_db.executeUpdate(sql7);
										if (flag)
										{
											String sql222 = (new StringBuilder("update purchase_details set invoice_check_tag='0' where purchase_ID='")).append(purchase_ID).append("' and provider_ID='").append(provider_ID[i]).append("'").toString();
											purchase_db.executeUpdate(sql222);
										}
									}
								} else
								if (Double.parseDouble(invoicing_sum2) != 0.0D)
								{
									provider_ID_control = provider_ID[i];
									invoiced_sum_control = Double.parseDouble(invoiced_sum[i]) + Double.parseDouble(invoicing_sum2);
									real_invoiced_sum += Double.parseDouble(invoicing_sum2);
									String sql = (new StringBuilder("update purchase_purchasing set real_contact_person='")).append(real_contact_person[i]).append("',real_contact_person_tel='").append(real_contact_person_tel[i]).append("',invoicing_sum='").append(invoicing_sum2).append("',remark='").append(remark[i]).append("',register='").append(register).append("',register_time='").append(register_time).append("' where id='").append(id[i]).append("'").toString();
									purchase_db.executeUpdate(sql);
									if (flag)
										if (invoiced_sum_control == Double.parseDouble(demand_cost_price_sum[i]))
										{
											String sql1 = (new StringBuilder("update purchase_details set invoiced_sum='")).append(invoiced_sum_control).append("',uninvoice_sum='0',invoice_tag='1' where purchase_ID='").append(purchase_ID).append("' and provider_ID='").append(provider_ID[i]).append("'").toString();
											purchase_db.executeUpdate(sql1);
										} else
										{
											double uninvoice_sum = Double.parseDouble(demand_cost_price_sum[i]) - invoiced_sum_control;
											String sql2 = (new StringBuilder("update purchase_details set invoiced_sum='")).append(invoiced_sum_control).append("',uninvoice_sum='").append(uninvoice_sum).append("',invoice_check_tag='0' where purchase_ID='").append(purchase_ID).append("' and provider_ID='").append(provider_ID[i]).append("'").toString();
											purchase_db.executeUpdate(sql2);
										}
								} else
								if (Double.parseDouble(invoicing_sum2) == 0.0D)
								{
									String sql7 = (new StringBuilder("delete from purchase_purchasing where id='")).append(id[i]).append("'").toString();
									purchase_db.executeUpdate(sql7);
									if (flag)
									{
										String sql222 = (new StringBuilder("update purchase_details set invoice_check_tag='0' where purchase_ID='")).append(purchase_ID).append("' and provider_ID='").append(provider_ID[i]).append("'").toString();
										purchase_db.executeUpdate(sql222);
									}
								}
							}

							if (rsList.size() == 0)
							{
								if (real_invoiced_sum == Double.parseDouble(demand_cost_price_sum_all))
								{
									String sql3 = (new StringBuilder("update purchase_purchase set invoiced_sum='")).append(real_invoiced_sum).append("',invoice_tag='3',invoice_check_tag='0' where purchase_ID='").append(purchase_ID).append("'").toString();
									purchase_db.executeUpdate(sql3);
									String sql21 = (new StringBuilder("select * from purchase_purchase where stock_gather_tag='3' and invoice_tag='3' and gather_tag='3' and check_tag='2' and purchase_ID='")).append(purchase_ID).append("'").toString();
									ResultSet rs21 = purchase_db.executeQuery(sql21);
									if (rs21.next())
									{
										String sql22 = (new StringBuilder("update purchase_purchase set purchase_tag='2' where purchase_ID='")).append(purchase_ID).append("'").toString();
										purchase_db.executeUpdate(sql22);
									}
									response.sendRedirect("draft/purchase/invoice_ok.jsp?finished_tag=7");
								} else
								{
									String sql4 = (new StringBuilder("update purchase_purchase set invoiced_sum='")).append(real_invoiced_sum).append("',invoice_check_tag='0' where purchase_ID='").append(purchase_ID).append("'").toString();
									purchase_db.executeUpdate(sql4);
									response.sendRedirect("draft/purchase/invoice_ok.jsp?finished_tag=6");
								}
							} else
							{
								String sql1 = (new StringBuilder("update purchase_purchase set invoice_check_tag='1',invoice_tag='2',invoice_time='")).append(invoice_time).append("' where purchase_ID='").append(purchase_ID).append("'").toString();
								purchase_db.executeUpdate(sql1);
								String sql;
								for (Iterator ite = rsList.iterator(); ite.hasNext(); purchase_db.executeUpdate(sql))
								{
									String elem[] = (String[])ite.next();
									sql = (new StringBuilder("insert into purchase_workflow(config_id,object_ID,describe1,describe2,invoice_time) values ('")).append(elem[0]).append("','").append(purchase_ID).append("','").append(elem[1]).append("','").append(elem[2]).append("','").append(invoice_time).append("')").toString();
								}

								response.sendRedirect("draft/purchase/invoice_ok.jsp?finished_tag=4");
							}
						} else
						{
							response.sendRedirect("draft/purchase/invoice_ok.jsp?finished_tag=5");
						}
					} else
					{
						response.sendRedirect((new StringBuilder("draft/purchase/invoice_ok_d.jsp?purchase_ID=")).append(purchase_ID).toString());
					}
				} else
				{
					response.sendRedirect((new StringBuilder("draft/purchase/invoice_ok_e.jsp?purchase_ID=")).append(purchase_ID).toString());
				}
				purchase_db.commit();
				purchase_db.close();
			} else
			{
				response.sendRedirect("error_conn.htm");
			}
		}
		catch (Exception ex)
		{
			ex.printStackTrace();
		}
	}
}
